package com.ruoyi.system.enums;

import lombok.Getter;

/**
 * 消息附件类型枚举
 * 用于消息推送中的附件类型分类
 */
@Getter
public enum MsgAttachTypeEnum {
    
    PDF(1, "PDF"),
    JPG(2, "JPG"),
    JPEG(3, "JPEG"),
    PNG(4, "PNG"),
    DWG(5, "DWG"),
    DXF(6, "DXF"),
    DOC(7, "DOC"),
    DOCX(8, "DOCX"),
    XLS(9, "XLS"),
    XLSX(10, "XLSX"),
    ZIP(11, "ZIP"),
    RAR(12, "RAR"),
    MP4(13, "MP4"),
    AVI(14, "AVI"),
    MP3(15, "MP3"),
    WAV(16, "WAV"),
    TXT(17, "TXT"),
    JSON(18, "JSON"),
    OTHER(99, "其他");

    private final Integer code;
    private final String description;

    MsgAttachTypeEnum(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 根据代码获取枚举
     */
    public static MsgAttachTypeEnum getByCode(Integer code) {
        for (MsgAttachTypeEnum type : values()) {
            if (type.getCode().equals(code)) {
                return type;
            }
        }
        return null;
    }

    /**
     * 根据描述获取枚举
     */
    public static MsgAttachTypeEnum getByDescription(String description) {
        for (MsgAttachTypeEnum type : values()) {
            if (type.getDescription().equals(description)) {
                return type;
            }
        }
        return null;
    }

    /**
     * 根据代码获取描述
     */
    public static String getDescription(Integer code) {
        MsgAttachTypeEnum type = getByCode(code);
        return type != null ? type.getDescription() : null;
    }

    /**
     * 根据描述获取代码
     */
    public static Integer getCode(String description) {
        MsgAttachTypeEnum type = getByDescription(description);
        return type != null ? type.getCode() : null;
    }

    /**
     * 根据文件扩展名获取枚举
     */
    public static MsgAttachTypeEnum getByFileExtension(String extension) {
        if (extension == null || extension.trim().isEmpty()) {
            return OTHER;
        }
        
        String ext = extension.trim().toUpperCase();
        for (MsgAttachTypeEnum type : values()) {
            if (type.getDescription().equals(ext)) {
                return type;
            }
        }
        return OTHER;
    }

    /**
     * 根据MIME类型获取枚举
     */
    public static MsgAttachTypeEnum getByMimeType(String mimeType) {
        if (mimeType == null || mimeType.trim().isEmpty()) {
            return OTHER;
        }
        
        String mime = mimeType.trim().toLowerCase();
        if (mime.contains("pdf")) return PDF;
        if (mime.contains("jpeg") || mime.contains("jpg")) return JPG;
        if (mime.contains("png")) return PNG;
        if (mime.contains("dwg")) return DWG;
        if (mime.contains("dxf")) return DXF;
        if (mime.contains("msword") || mime.contains("word")) return DOC;
        if (mime.contains("officedocument.wordprocessingml")) return DOCX;
        if (mime.contains("excel") || mime.contains("spreadsheetml")) return XLSX;
        if (mime.contains("zip")) return ZIP;
        if (mime.contains("rar")) return RAR;
        if (mime.contains("mp4")) return MP4;
        if (mime.contains("avi")) return AVI;
        if (mime.contains("mp3")) return MP3;
        if (mime.contains("wav")) return WAV;
        if (mime.contains("text/plain")) return TXT;
        if (mime.contains("json")) return JSON;
        
        return OTHER;
    }
}
